package com.mooc.lib_find.ui.find_ist

import android.view.LayoutInflater
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.viewpager.widget.ViewPager
import com.flyco.tablayout.SlidingTabLayout
import com.mooc.lib_common.mvvm.BaseFragment
import com.mooc.lib_find.BR
import com.mooc.lib_find.R
import com.mooc.lib_find.adapter.FindListAdapter
import com.mooc.lib_find.adapter.FindVPAdapter
import com.mooc.lib_find.databinding.FragmentFindBinding
import com.mooc.lib_find.databinding.LayoutListFindBinding
import com.mooc.lib_find.model.FindTabEntity

class FindFragment : BaseFragment<FindViewModel,FragmentFindBinding>(){

    //关注列表获取参数:type:onlyFollow;userId:1578644405;tagId:1
    //推荐列表获取参数:type:excludeFollow;tagId:2
    //配置flycotab虚拟数据
    lateinit var tabs : ArrayList<FindTabEntity>
    lateinit var tab : SlidingTabLayout
    lateinit var vp : ViewPager
    lateinit var vpAdapter : FindVPAdapter
    lateinit var followAdapter: FindListAdapter
    lateinit var recommandAdapter: FindListAdapter

    init {
        //创建tab标签页卡对应数据源
        tabs = java.util.ArrayList()
        tabs.add(FindTabEntity(1,"onlyFollow","关注"))
        tabs.add(FindTabEntity(2,"excludeFollow","推荐"))
    }

    override fun initView() {
        tab = v.findTab
        vp = v.findVp
        followAdapter = FindListAdapter()
        recommandAdapter = FindListAdapter()
        recommandAdapter.setOnItemChildClickListener(vm)
        val followView = DataBindingUtil.inflate<LayoutListFindBinding>(
            LayoutInflater.from(context),
            R.layout.layout_list_find,
            null,false
        )
        followView.findSmart.tag = "onlyFollow"
        val pool = followView.findRv.recycledViewPool
        followAdapter.tag = "onlyFollow"
        followView.setVariable(BR.listener,vm)
        followView.setVariable(BR.findadapter,followAdapter)
        val recommandView = DataBindingUtil.inflate<LayoutListFindBinding>(
            LayoutInflater.from(context),
            R.layout.layout_list_find,
            null,false
        )
        recommandView.findRv.setRecycledViewPool(pool)
        recommandView.findSmart.tag = "excludeFollow"
        recommandAdapter.tag = "excludeFollow"
        recommandView.setVariable(BR.listener,vm)
        recommandView.setVariable(BR.findadapter,recommandAdapter)
        val list = java.util.ArrayList<View>()
        list.add(followView.root)
        list.add(recommandView.root)
        vpAdapter = FindVPAdapter(tabs,list)
        vp.adapter = vpAdapter
        tab.setViewPager(vp)
    }

    override fun bindLayout(): Int {
        return R.layout.fragment_find
    }

    override fun initData() {
        bindData()
        vm.refreshFollow()
        vm.refreshRecommand()

    }

    fun bindData(){
        vm.refreshFollowBus.observe(this,{
            followAdapter.setNewInstance(it.data)
        })
        vm.refreshRecommandBus.observe(this,{
            recommandAdapter.setNewInstance(it.data)

        })
        vm.loadFollowBus.observe(this,{
            followAdapter.addData(it.data!!)

        })
        vm.loadRecommandBus.observe(this,{
            recommandAdapter.addData(it.data!!)
        })
        vm.followBus.observe(this,{
            recommandAdapter.removeAt(it.position)
            followAdapter.addData(it.entity!!)

        })
    }


}